package com.examples.list;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ListSelectionListener {

	public static void main(String[] args) {

		Display d = Display.getDefault();
		Shell s = new Shell(d);
		s.setLayout(new FillLayout());

		final List l = new List(s, SWT.MULTI);
		for (int i = 0; i < 10; i++) {
			l.add("Item number " + i);
		}

		final Text t = new Text(s, SWT.NONE);
		t.setText("Selected items");
		t.setBounds(60, 130, 160, 25);

		l.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int[] selectedItems = l.getSelectionIndices();
				t.setText("Selected items");
				for (int s : selectedItems) {
					t.setText(t.getText()+" "+s);
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		 s.open();
		    while (!s.isDisposed()) {
		      if (!d.readAndDispatch())
		        d.sleep();
		    }
		  //d.dispose(); //Commented only for SWTShowcase application.
	}

}
